package com.sunsun_china.www.aq;

import java.util.ArrayList;
import java.util.HashMap;

import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.sunsun_china.www.aq.R;
import com.sunsun_china.www.aq.base.AqDeviceInfo;
import com.sunsun_china.www.aq.base.MyApplication;


/**
 * 日期：2015-05-05
 * 		1. 自动同步设备名称
 * 日期：2015-05-12
 * 		1. 点击已经添加的设备提示到设备列表中连接设备
 */
@SuppressLint("NewApi") public class AddDeviceActivity extends ActionBarActivity {

	ListView mListView;
	MyApplication mApp;
	Context mContext;
	AqDeviceInfo mSelectDeviceInfo;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_add_device);
		getSupportActionBar().setDisplayShowHomeEnabled(false);
		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setDisplayShowTitleEnabled(true);
		mApp = (MyApplication) getApplication();
		mApp.mAddDeviceUi = this;
		mContext = this;
		mSelectDeviceInfo = null;
		mListView = (ListView) findViewById(R.id.add_device_listView);
		// 列表项短按键处理
		mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
				mSelectDeviceInfo = mApp.mLanDeviceList.get(position);
				if (deviceIsAdd(mSelectDeviceInfo)) {
					new AlertDialog.Builder(mContext)
							.setTitle("温馨提示")
							.setMessage("请在设备列表中连接设备！")
							.setPositiveButton("确定", null)
							.show();
					return;
				}
				new AlertDialog.Builder(mContext)
						.setTitle("添加确认")
						.setMessage("是否添加设备？")
						.setPositiveButton("否", null)
						.setNegativeButton("是", new DialogInterface.OnClickListener() {
							@Override
							public void onClick(DialogInterface dialog, int which) {
								mApp.addDevice(mSelectDeviceInfo);
								refreshDeviceList();
							}
						})
						.show();
			}
		});
		refreshDeviceList();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.menu_add_device, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.add_device_action_settings1) {
			// 刷新按键，清空局域网设备列表，重新等待设备
			mApp.mLanDeviceList.clear();
			refreshDeviceList();
			return true;
		}
		if (id == android.R.id.home) {
			// 返回
			mApp.mAddDeviceUi = null;
			this.finish();
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		// 如果是返回键
		if(keyCode == KeyEvent.KEYCODE_BACK){
			mApp.mAddDeviceUi = null;
		}
		return super.onKeyDown(keyCode, event);
	}

	public boolean deviceIsAdd(AqDeviceInfo deviceInfo) {
		for (int i = 0; i < mApp.mMyDeviceList.size(); i++) {
			AqDeviceInfo d = mApp.mMyDeviceList.get(i);
			if (deviceInfo.mDid.equalsIgnoreCase(d.mDid)) {
				deviceInfo.mDeviceName = d.mDeviceName;
				return true;
			}
		}
		return false;
	}

	/**
	 * 刷新局域网设备列表
	 */
	public void refreshDeviceList() {
		ArrayList<HashMap<String, Object>> listItems = new ArrayList<HashMap<String, Object>>();
		for (int i=0; i<mApp.mLanDeviceList.size(); i++) {
			AqDeviceInfo deviceInfo = mApp.mLanDeviceList.get(i);
			HashMap<String, Object> map = new HashMap<String, Object>();
			map.put("ItemDid", "DID : " + deviceInfo.mDid);
			if (deviceIsAdd(deviceInfo)) {
				map.put("ItemState1", "");
				map.put("ItemState2", "已添加");
			} else {
				map.put("ItemState1", "未添加");
				map.put("ItemState2", "");
			}
			map.put("ItemName", deviceInfo.mDeviceName);
			map.put("ItemRightArrow", null);
			listItems.add(map);
		}
		SimpleAdapter listItemsAdapter = new SimpleAdapter(this, listItems, R.layout.device_item,
				new String[] {"ItemName", "ItemDid", "ItemState1", "ItemState2", "ItemRightArrow"},
				new int[] {R.id.textView_dev_name, R.id.textView_dev_did, R.id.textView_dev_state1, R.id.textView_dev_state2, R.id.imageView_dev_right_arrow});
		mListView.setAdapter(listItemsAdapter);
	}
}
